package com.jijs.ws.interceptor;

import java.util.List;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.databinding.DataBinding;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.jaxb.JAXBDataBinding;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.jijs.ws.bean.User;

public class LicenseUserInInterceptor extends AbstractSoapInterceptor {

	//http://cxf.apache.org/docs/interceptors.html
	//阶段
	public LicenseUserInInterceptor() {
		super(Phase.POST_LOGICAL);
	}

	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		try {
			List<Header> headers = message.getHeaders();
			DataBinding db = new JAXBDataBinding(User.class);
			Object o = null;
			for (Header h : headers) {
				o = h.getObject();
				if(o instanceof Node){
					Element e = (Element)o;
					Node un = e.getElementsByTagName("username").item(0);
					Node pn = e.getElementsByTagName("password").item(0);
					System.out.println(un.getTextContent()+","+pn.getTextContent());
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
